package br.com.dufs.mvc.dao.impl;

import java.math.BigDecimal;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import br.com.dufs.mvc.dao.interfaces.BovinoDAO;
import br.com.dufs.mvc.model.Bovino;

import com.fscoreunipe.mvc.dao.impl.generico.DaoGenericoImpl;

@Repository("bovinoDAO")
public class BovinoDAOImpl extends DaoGenericoImpl<Bovino, Integer> implements
		BovinoDAO {

	private Query query;

	@SuppressWarnings("unchecked")
	@Transactional(readOnly = false)
	@Override
	public List<Bovino> listaPorCPF(String id) {
		// TODO Auto-generated method stub
		List<Bovino> retorno = null;

		this.query = getSessionFactory().getCurrentSession().createQuery(
				"From Bovino where  entrevistado='" + id + "'");

		if (this.query.list().size() > 0) {
			retorno = this.query.list();
		}
		return retorno;

	}

	@Transactional(readOnly = false)
	@Override
	public int quantidade(String id) {
		// TODO Auto-generated method stub

		int retorno = 0;

		this.query = getSessionFactory().getCurrentSession().createSQLQuery(
				"Select SUM(b.qnt) From Bovino b where  entrevistado_cpf='" + id
						+ "'");

		if (this.query.uniqueResult() != null) {
			BigDecimal a = (BigDecimal)this.query.uniqueResult();
			
			retorno = a.intValue();
		}

		return retorno;
	}

	@Transactional(readOnly = false)
	@Override
	public double total(String id) {
		// TODO Auto-generated method stub
		double retorno = 0;

		this.query = getSessionFactory().getCurrentSession().createSQLQuery(
				"Select SUM(qnt * valorUniatrio) From Bovino b where  entrevistado_cpf='" + id
						+ "'");

		if (this.query.uniqueResult() != null) {
			Double a = (Double)this.query.uniqueResult();
			retorno = a;
		}

		return retorno;
	}

}
